<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>部门选择</title>
    <meta name="ctx" th:content="${#httpServletRequest.getContextPath()}"/>
    <link rel="stylesheet" th:href="@{/css/admin/base-v2.css}">
    <link rel="stylesheet" th:href="@{/lib/font-awesome/4.7.0/css/font-awesome.min.css}">
    <link rel="stylesheet" th:href="@{/css/admin/app.css}">
</head>

<body>

<div class="app-page">

    <div class="app-page-content">

        <div class="app-card">
            <div class="app-card-body">
                <div class="app-card-body-con">
                    <div class="ztree"></div>
                </div>
            </div>
        </div>

    </div>

</div>

<script th:src="@{/lib/jquery.min.js}"></script>
<script th:src="@{/lib/layer-v3.1.1/layer/layer.js}"></script>
<script th:src="@{/js/popup.js}"></script>
<script th:src="@{/js/form.js}"></script>
<script th:src="@{/js/admin/app.js}"></script>
<script th:src="@{/js/pagination.js}"></script>
<script th:src="@{/js/tree.js}"></script>
<script type="text/javascript" th:inline="javascript">
    //获取context path
    var context = $("meta[name='ctx']").attr("content") || '';
    var tree = null;
    $(function () {
        initTree();
    })

    function initTree() {
        var url = context + "/department/listDepartmentTree";
        //权限列表
        $.ajax({
            type: "POST",
            url: url,
            success: function (obj) {
                var code = obj.code;
                if (code == 0) {
                    setTree(obj.data);
                }
            }
        })
    }

    function setTree(data) {
        // 树形控件
        tree = new Tree('.ztree', {
            type: 1,
            data: data,
            linkage: false,
            multiple: false
        })
    }

    // 获取树形控件数据
    function getTreeData() {
        var idsdata = [];
        var namedata = [];
        var checked = checkTreeLength();
        if (!checked) {
            return false;
        } else {
            tree.forEach(function (treeItem) {
                if (treeItem.checked) {
                    idsdata.push(treeItem.departmentId);
                    namedata.push(treeItem.label);
                }
            })
            return {
                idsdata: idsdata.join(','),
                namedata: namedata.join(',')
            };
        }

    }

    function checkTreeLength() {
        if (tree.checked.length > 1) {
            alert("只能选择一个部门");
            return false;
        }
        return true;
    }

</script>
</body>

</html>